package com.test.prolenders.activity_prolenders;

import static com.test.prolenders.config_prolenders.PermissionConfig_prolenders.PermissionCode_PhoneState;
import static com.test.prolenders.config_prolenders.PermissionConfig_prolenders.PermissionCode_Sms;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.test.prolenders.R;
import com.test.prolenders.adapter_prolenders.PermissionViewAdapter_prolenders;
import com.test.prolenders.base_prolenders.BaseActivity_prolenders;
import com.test.prolenders.bean_prolenders.PermissionInfo_prolenders;
import com.test.prolenders.dialog_prolenders.KindTipDialog_prolenders;
import com.test.prolenders.listener_prolenders.PermissionListener_prolenders;
import com.test.prolenders.status_prolenders.PermissionStatus_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public class PermissionActivity_prolenders extends BaseActivity_prolenders implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {

    private ViewPager2 permissionVp2;
    private TextView permissionTitleTv;
    private View btnView,agreeView,denyView;
    private Button denyBtn,agreeBtn;

    private PermissionViewAdapter_prolenders permissionViewAdapterProlenders;
    private final ArrayList<PermissionInfo_prolenders> infos = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_prolenders);
        page = "PERMISSION";

        findViewById(R.id.aps_ib_back).setOnClickListener(v -> onBackPressed());

        permissionVp2 = findViewById(R.id.aps_vp2_permission);
        permissionTitleTv = findViewById(R.id.aps_tv_permission_title);
        btnView = findViewById(R.id.aps_view_btn);
        agreeView = findViewById(R.id.aps_view_agree);
        denyView = findViewById(R.id.aps_view_deny);
        denyBtn = findViewById(R.id.aps_btn_deny);
        agreeBtn = findViewById(R.id.aps_btn_agree);

        init();
    }

    private void init(){


        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)){

            infos.add(new PermissionInfo_prolenders(
                    R.drawable.img_permission_sms_prolenders,
                    getString(R.string.permission_sms_title_prolenders),
                    getString(R.string.permission_sms_text_prolenders),
                    smsPermissionListneer,
                    "SMS")
            );

            infos.add(new PermissionInfo_prolenders(
                    R.drawable.img_permission_applist_prolenders,
                    getString(R.string.permission_applist_title_prolenders),
                    getString(R.string.permission_applist_text_prolenders),
                    applistPermissionListneer,
                    "APPS")
            );

        }

        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE)){
            infos.add(new PermissionInfo_prolenders(
                    R.drawable.img_permission_phone_state_prolenders,
                    getString(R.string.permission_phonestate_title_prolenders),
                    getString(R.string.permission_phonestate_text_prolenders),
                    phonestatePermissionListneer,
                    "PHONE_STATE")
            );
        }

        if (!infos.isEmpty()) permissionTitleTv.setText(infos.get(0).getTitle());

        permissionViewAdapterProlenders = new PermissionViewAdapter_prolenders(infos);
        permissionVp2.setAdapter(permissionViewAdapterProlenders);

        //页面切换监听
        permissionVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                if (position<0 || position>=infos.size()) return;

                PermissionInfo_prolenders info = infos.get(position);

                permissionTitleTv.setText(info.getTitle());

                switch (info.getStatus()){
                    case Agree:
                        agreeView.setVisibility(View.VISIBLE);
                        denyView.setVisibility(View.GONE);
                        btnView.setVisibility(View.GONE);
                        break;
                    case Deny:
                        agreeView.setVisibility(View.GONE);
                        denyView.setVisibility(View.VISIBLE);
                        btnView.setVisibility(View.GONE);
                        break;
                    case Default:
                    default:
                        agreeView.setVisibility(View.GONE);
                        denyView.setVisibility(View.GONE);
                        btnView.setVisibility(View.VISIBLE);

                        denyBtn.setOnClickListener(v -> {
                            agreeView.setVisibility(View.GONE);
                            denyView.setVisibility(View.VISIBLE);
                            btnView.setVisibility(View.GONE);
                            info.setStatus(PermissionStatus_prolenders.Deny);
                            info.onPermissionDeny();
                        });

                        //权限同意
                        agreeBtn.setOnClickListener(v -> info.onPermissionAgree());

                        break;
                }

            }
        });

    }

    private final PermissionListener_prolenders smsPermissionListneer = new PermissionListener_prolenders() {
        @Override
        public void onPermissionAgree() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_AGREE_CLICK","");
            new KindTipDialog_prolenders(PermissionActivity_prolenders.this,
                    () -> HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_CHECKBOX_DIALOG_YES_CLICK","");
                        EasyPermissions.requestPermissions(PermissionActivity_prolenders.this, getString(R.string.launch_permissions_sms_prolenders), PermissionCode_Sms, Manifest.permission.READ_SMS);
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_DENY_CLICK","");
            next();
        }

        @Override
        public void onNextClick() {
            next();
        }
    };

    private final PermissionListener_prolenders applistPermissionListneer = new PermissionListener_prolenders() {
        @Override
        public void onPermissionAgree() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"APPS_AGREE_CLICK","");
            new KindTipDialog_prolenders(PermissionActivity_prolenders.this,
                    () -> HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"APPS_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"APPS_CHECKBOX_DIALOG_YES_CLICK","");
                        int p = permissionVp2.getCurrentItem();
                        if (p>=0&&p<infos.size()) infos.get(p).setStatus(PermissionStatus_prolenders.Agree);
                        next();
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"APPS_DENY_CLICK","");
            next();
        }

        @Override
        public void onNextClick() {
            next();
        }
    };

    private final PermissionListener_prolenders phonestatePermissionListneer = new PermissionListener_prolenders() {
        @Override
        public void onPermissionAgree() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_AGREE_CLICK","");
            new KindTipDialog_prolenders(PermissionActivity_prolenders.this,
                    () -> HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_CHECKBOX_DIALOG_YES_CLICK","");EasyPermissions.requestPermissions(PermissionActivity_prolenders.this, getString(R.string.launch_permissions_phonestate_prolenders), PermissionCode_PhoneState, Manifest.permission.READ_PHONE_STATE);
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny() {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_DENY_CLICK","");
            out();
        }

        @Override
        public void onNextClick() {
            next();
        }
    };


    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        switch (requestCode){
            case PermissionCode_PhoneState:

                HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_2_DENY_CLICK","");
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this).build().show();
                }else out();
                break;
            case PermissionCode_Sms:

                HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_2_DENY_CLICK","");
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this).build().show();
                }else next();

                break;
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        switch (requestCode){
            case PermissionCode_PhoneState:
                HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"PHONE_STATE_2_AGREE_CLICK","");

                int p1 = permissionVp2.getCurrentItem();
                if (p1>=0&&p1<infos.size()) infos.get(p1).setStatus(PermissionStatus_prolenders.Agree);

                next();
                break;
            case PermissionCode_Sms:
                HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"SMS_2_AGREE_CLICK","");

                int p2 = permissionVp2.getCurrentItem();
                if (p2>=0&&p2<infos.size()) infos.get(p2).setStatus(PermissionStatus_prolenders.Agree);

                next();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        switch (requestCode){
            case PermissionCode_PhoneState:
                EasyPermissions.requestPermissions(this, this.getString(R.string.launch_permissions_phonestate_prolenders), PermissionCode_PhoneState, Manifest.permission.READ_PHONE_STATE);
                break;
            case PermissionCode_Sms:
                EasyPermissions.requestPermissions(this, this.getString(R.string.launch_permissions_sms_prolenders), PermissionCode_Sms, Manifest.permission.READ_SMS);
                break;
        }
    }

    @Override
    public void onRationaleDenied(int requestCode) {}

    public void next(){
        int nextPageIndex = permissionVp2.getCurrentItem()+1;
        if (nextPageIndex>=0&& permissionViewAdapterProlenders.getItemCount()>nextPageIndex) permissionVp2.setCurrentItem(nextPageIndex);
        else finishPermissionProcess();
    }

    private void finishPermissionProcess(){
        setResult(RESULT_OK);
        finish();
    }

    private void out(){
        setResult(RESULT_OUT_APP);
        finish();
    }


}
